matrix R_nofe = J(10,3,.)
local myvarlist math_std read_std
matrix colnames R_nofe = R2 `myvarlist'
matrix R_fe = R_nofe
local j = 2

foreach outcomevar of varlist `myvarlist' {
	eststo clear
	estimates clear
	preserve
		quiet: tab grade, g(_grade_)
		
		if "`outcomevar'" == "math_std" {
			keep if subject == "Math"
			local outcomename "m"
			}
		else if "`outcomevar'" == "read_std" {
			keep if subject == "ELA"
			local outcomename "r"
			}
		
		quietly: reg `outcomevar' nothreat_cycle3 $experiencevar $sdemovars $prevvars i.year i.sch_id i.id $yeardosevar _grade_1 _grade_2
		
		
		
		local i = 1
		forvalues thisr = 0.5(0.05)1 {
			
			* ALLL TREATMENTS -- remove teacher from un-controlled (i.e. allow for selection on teachers -- not sure this makes sense. How do you run the un-controlled regression without a teacher FE?)
			capture: psacalc delta nothreat_cycle3, rmax(`thisr') mcontrol(_grade_1 _grade_2 i.year $yeardosevar i.sch_id)
			
			
			di r(delta)
			matrix R_nofe[`i',1] = `thisr'
			matrix R_nofe[`i',`j'] = abs(r(delta))
			
			* ALLL TREATMENTS -- keep teachers in un-controlled (i.e. only look at selection on student unobservables)
			capture: psacalc delta nothreat_cycle3, rmax(`thisr') mcontrol(_grade_1 _grade_2 i.year $yeardosevar i.sch_id i.id)
			
			di r(delta)
			matrix R_fe[`i',1] = `thisr'
			matrix R_fe[`i',`j'] = abs(r(delta))
			
			local ++i
			}
		local ++j
		
		
	restore
	}
	matrix colnames R_nofe = "R-Squared" "Math" "Reading"
	matrix colnames R_fe = "R-Squared" "Math" "Reading"
		matrix list R_nofe
	outtable using "$output_dir\Tables\_Table_22_nofe", mat(R_nofe) replace ///
		nobox center nodots norowlab f(%3.2f %4.2f %4.2f)
	matrix list R_fe
	outtable using "$output_dir\Tables\_Table_22_fe", mat(R_fe) replace ///
		nobox center nodots norowlab f(%3.2f %4.2f %4.2f)
